import time
import os
from datetime import datetime
from datetime import timedelta

#time
#获取当前时间
print(time.localtime())

#时间戳
print(time.time())

#时间格式转换
#time.struct_time -> str
print(time.strftime("%Y-%m-%d %H:%M:%S"))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))

#str -> time.struct_time
print(time.strptime("2020-11-9 17:44:23", "%Y-%m-%d %H:%M:%S"))

#time.struct_time -> tuple
now = time.localtime()
print(tuple(now))                     #时间格式到字符串

#tuple -> time.struct_time
print(time.struct_time(tuple(now)))   #字符串到时间格式
print(list(now))                      #时间格式到字符串

#timestamp  -> time.struct_time
print(time.localtime(time.time()))  #提供了时区接口
print(time.gmtime(time.time()))     #没有时区

#time.struct_time -> timestamp
print(time.mktime(time.localtime()))


#datetime
#查看时间
print(datetime.today())
print(datetime.now())
print(datetime.utcnow())

#时间格式的转换
#datetime.datetime -> str
now = datetime.now()
print(now)
print(now.strftime("%Y-%m-%d %H:%M:%S"))

#str -> datetime.datetime
now_str = now.strftime("%Y-%m-%d %H:%M:%S")
print(datetime.strptime(now_str, "%Y-%m-%d %H:%M:%S"))

#datetime.datetime -> timetuple -> tuple
print(now.timetuple())
print(tuple(now.timetuple()))

#tuple -> datetime.datetime
t = tuple(now.timetuple())
new_t = t[:-3]
print(new_t)
print(datetime(*new_t))

#datetime.datetime -> timestamp
print(now.timestamp())

#timestamp -> datetime.datetime
print(datetime.fromtimestamp(now.timestamp()))

#时间运算
print(now)
print(now+timedelta(hours=1))
print(now+timedelta(days=1))

def get_days(days):
    days = int(days)
    time = datetime.today()
    time += timedelta(days = days)
    return time.strftime("%Y-%m-%d")

print(get_days(-1))